package wwy.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wwy.Common.LogicService;
import wwy.Dao.OrderItemsDao;
import wwy.Dto.CommonDto.FoodsDto;
import wwy.Dto.OrderItemsDto;
import wwy.Entity.CommonEntity.Foods;
import wwy.Entity.OrderItem;
import wwy.Service.OrderService;
import wwy.feign.getMerchantService;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderItemsService extends LogicService<OrderItemsDao, OrderItem, Long> {
    private final getMerchantService merchantService;
    private final OrderService orderService;

    @Autowired
    public OrderItemsService(OrderItemsDao dao, getMerchantService merchantService,
                             @Lazy OrderService orderService
    ) {super(dao); this.merchantService = merchantService; this.orderService = orderService;}

    @Transactional
    public List<OrderItemsDto> getOrderItemsByOrderId(Long orderId){
        List<OrderItem> orderItems = dao.getOrderItemsByOrderId(orderId);
        List<OrderItemsDto> orderItemsDtoS = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            OrderItemsDto orderItemsDto = modelMapper.map(orderItem, OrderItemsDto.class);
            orderItemsDto.setFood(orderItem.getFood().getName());
            orderItemsDtoS.add(orderItemsDto);
        }
        return orderItemsDtoS;
    }
    @Transactional
    public void addNewOrderItem(OrderItemsDto orderItemDto, Long orderId,String merchantName){
        OrderItem newOrderItem = modelMapper.map(orderItemDto, OrderItem.class);
        Long merchantId = merchantService.getMerchantByName(merchantName).getId();
        FoodsDto foodsDto = merchantService.getFood(merchantId,orderItemDto.getFood());
        newOrderItem.setFood(modelMapper.map(foodsDto, Foods.class));
        newOrderItem.setOrder(orderService.GET(orderId));
        dao.save(newOrderItem);
    }

    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(2)
    public void addDefaultOrderItems(){
        if(dao.count() > 0) return;
        OrderItem item1 = new OrderItem();
        item1.setQuantity(2);
        item1.setPrice(30.0);
        item1.setOrder(orderService.GET(1L));
        FoodsDto foodsDto = merchantService.getFood(1L, "纯肉鲜肉（水饺）");
        item1.setFood(modelMapper.map(foodsDto, Foods.class));
        dao.save(item1);

        OrderItem item2 = new OrderItem();
        item2.setQuantity(1);
        item2.setPrice(16.0);
        item2.setOrder(orderService.GET(1L));
        FoodsDto foodsDto2 = merchantService.getFood(1L, "玉米鲜肉（水饺）");
        item2.setFood(modelMapper.map(foodsDto2, Foods.class));
        dao.save(item2);

        OrderItem item3 = new OrderItem();
        item3.setQuantity(1);
        item3.setPrice(15.0);
        item3.setOrder(orderService.GET(2L));
        FoodsDto foodsDto3 = merchantService.getFood(1L, "纯肉鲜肉（水饺）");
        item3.setFood(modelMapper.map(foodsDto3, Foods.class));
        dao.save(item3);

        OrderItem item4 = new OrderItem();
        item4.setQuantity(1);
        item4.setPrice(16.0);
        item4.setOrder(orderService.GET(2L));
        FoodsDto foodsDto4 = merchantService.getFood(1L, "玉米鲜肉（水饺）");
        item4.setFood(modelMapper.map(foodsDto4, Foods.class));
        dao.save(item4);

        OrderItem item5 = new OrderItem();
        item5.setQuantity(1);
        item5.setPrice(8.0);
        item5.setOrder(orderService.GET(3L));
        FoodsDto foodsDto5 = merchantService.getFood(2L, "蛋黄焗豆花");
        item5.setFood(modelMapper.map(foodsDto5, Foods.class));
        dao.save(item5);

        OrderItem item6 = new OrderItem();
        item6.setQuantity(1);
        item6.setPrice(14.0);
        item6.setOrder(orderService.GET(3L));
        FoodsDto foodsDto6 = merchantService.getFood(2L, "麻婆豆腐");
        item6.setFood(modelMapper.map(foodsDto6, Foods.class));
        dao.save(item6);

        OrderItem item7 = new OrderItem();
        item7.setQuantity(1);
        item7.setPrice(12.0);
        item7.setOrder(orderService.GET(3L));
        FoodsDto foodsDto7 = merchantService.getFood(2L, "广式小豆腐");
        item7.setFood(modelMapper.map(foodsDto7, Foods.class));
        dao.save(item7);

        OrderItem item8 = new OrderItem();
        item8.setQuantity(2);
        item8.setPrice(16.0);
        item8.setOrder(orderService.GET(4L));
        FoodsDto foodsDto8 = merchantService.getFood(2L, "蛋黄焗豆花");
        item8.setFood(modelMapper.map(foodsDto8, Foods.class));
        dao.save(item8);

        OrderItem item9 = new OrderItem();
        item9.setQuantity(1);
        item9.setPrice(14.0);
        item9.setOrder(orderService.GET(4L));
        FoodsDto foodsDto9 = merchantService.getFood(2L, "麻婆豆腐");
        item9.setFood(modelMapper.map(foodsDto9, Foods.class));
        dao.save(item9);

        OrderItem item10 = new OrderItem();
        item10.setQuantity(1);
        item10.setPrice(12.0);
        item10.setOrder(orderService.GET(4L));
        FoodsDto foodsDto10 = merchantService.getFood(2L, "广式小豆腐");
        item10.setFood(modelMapper.map(foodsDto10, Foods.class));
        dao.save(item10);
    }

}
